//
//  InterstitialAdManager.m
//  bwt_ad_flutter
//
//  Created by ciba on 2024/8/29.
//

#import "BwtInterstitialAdManager.h"
#import "BwtInterstitialAd.h"
#import "BwtAdLogManager.h"

@implementation BwtInterstitialAdManager

/// 单例
+ (BwtInterstitialAdManager *)getBwtInterstitialAdManager {
    static BwtInterstitialAdManager *bwtInterstitialAdManager;
    if(!bwtInterstitialAdManager) {
        bwtInterstitialAdManager = [[BwtInterstitialAdManager alloc] init];
        [bwtInterstitialAdManager initData];
    }
    return bwtInterstitialAdManager;
}

/// 初始化
- (void)initData {
    if (!_interstitialAdMap) {
        _interstitialAdMap = [[NSMutableDictionary alloc] initWithCapacity:0];
    }
}

/// 加载插屏广告
- (void)loadInterstitialAd:(AdParams*)adParams loadThenShow:(bool)loadThenShow isRequestIdfa:(bool)isRequestIdfa {
    BwtInterstitialAd *interstitialAd = [BwtInterstitialAd alloc];
    interstitialAd.adParams = adParams;
    interstitialAd.loadThenShow = loadThenShow;
    interstitialAd.isRequestIdfa = isRequestIdfa;
    
    NSString *adId = adParams.adId;
    [_interstitialAdMap setObject:interstitialAd forKey:adId];
    
    [[BwtAdLogManager getBwtAdLogManager] log:@"开始加载插屏广告, adId: %@", adParams.adId];
    [interstitialAd loadInterstitialAd];
}

/// 展示插屏广告
- (void)showInterstitialAd:(AdParams*)adParams {
    NSString *adId = adParams.adId;
    [[BwtAdLogManager getBwtAdLogManager] log:@"尝试展示插屏广告, adId: %@", adId];
    if (!adId) {
        return;
    }
    BwtInterstitialAd *interstitialAd = _interstitialAdMap[adId];
    if (interstitialAd) {
        [interstitialAd showInterstitialAd];
    }
}

/// 释放插屏广告
- (void)releaseAd:(NSString*)adId {
    if (adId) {
        BwtInterstitialAd *interstitialAd = _interstitialAdMap[adId];
        if (interstitialAd) {
            [interstitialAd releaseAd];
            [_interstitialAdMap removeObjectForKey:adId];
            
            [[BwtAdLogManager getBwtAdLogManager] log:@"插屏广告已释放, adId: %@", adId];
        }
    }
}
@end
